package com.wfk.plat.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.image.common.RestUploadFileInfo;
import com.wfk.constant.PathConstant;
import com.wfk.entity.AppBanners;
import com.wfk.entity.BaseInfo;
import com.wfk.entity.SysCashDetails;
import com.wfk.entity.SysCashRule;
import com.wfk.plat.annotation.AuthResource;
import com.wfk.plat.mapping.MappingUtils;
import com.wfk.service.IAppBannersService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.ISysCashRuleService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;

@Controller
public class SystemController {
	@Autowired
	private IBaseInfoService baseInfoService ;
	@Autowired
	private ISysCashRuleService sysCashRuleService;
	@Autowired
	public IAppBannersService  appBannersService;
	
	
	
	@AuthResource(url=MappingUtils.appbannerList)
	@RequestMapping(value=MappingUtils.appbannerAdd)
	public ModelAndView appbannerAdd(Integer loginId) {
		ModelAndView view = new ModelAndView(MappingUtils.appbannerAdd);
		view.addObject("loginId",loginId);
		return view;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.appbannerList)
	public ModelAndView appbannerList(Integer loginId) {
		ModelAndView view = new ModelAndView(MappingUtils.appbannerList);
		view.addObject("list",appBannersService.list(null));
		view.addObject("loginId",loginId);
		return view;
	}
	
	@AuthResource(url=MappingUtils.appbannerList)
	@RequestMapping(value=MappingUtils.appbannerDev)
	@ResponseBody
	public ResponseData appbannerDev(Integer id,Integer loginId){
		this.appBannersService.removeById(id);
		return ResponseData.ok();
	}
	
	@RequestMapping(value=MappingUtils.appbannerEdit)
	@ResponseBody
	public ResponseData appbannerEdit(AppBanners app,MultipartFile upfile) throws IOException{
		if(upfile == null) {
			return ResponseData.baseError("上传失败") ;
		}
		RestUploadFileInfo upload = com.image.common.Service.uploadImage(upfile.getOriginalFilename(), upfile.getInputStream(), PathConstant.bank_image);
		if(upload.isStatus()) {
			app.setIcon( upload.getServiceName() + upload.getFilePath() + upload.getFileName());
		}else {
			return ResponseData.baseError("图片上传失败") ;
		}
		this.appBannersService.saveOrUpdate(app);
		return ResponseData.ok();
	}
	
	/**
	 * 提现设置页面
	 * @param id
	 * @return
	 */
	@RequestMapping(value=MappingUtils.showCashRule)
	public ModelAndView showCashRule(Integer tabIndex){
		 //用户
		 SysCashRule customerCashRule= sysCashRuleService.getOne(null);
		 ModelAndView view=new ModelAndView(MappingUtils.showCashRule);
		 view.addObject("cashSet", customerCashRule);
		 view.addObject("tabIndex", tabIndex);
		return view;
	}
	
	/**
	 * 设置提现规则
	 * @param request
	 * @param cashSet
	 * @return
	 */
	@RequestMapping(value=MappingUtils.editCashRule)
	@ResponseBody
	public ResponseData editCashSet(HttpServletRequest request,SysCashRule cashSet){
		ResponseData result=sysCashRuleService.editCashRule(cashSet);
		return result;
	}
	
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showSystemBase)
	public ModelAndView showSystemBase(Integer loginId) {
		
		List<BaseInfo> infoList = baseInfoService.list(null);
		ModelAndView m = new ModelAndView(MappingUtils.showSystemBase);
		m.addObject("infoList", infoList) ;
		m.addObject("loginId", loginId) ;
		return m ;
	}

	@AuthResource
	@ResponseBody
	@RequestMapping(value=MappingUtils.doEditSystemBase,method= {RequestMethod.POST})
	public ResponseData doEditSystemBase(BaseInfo baseInfo) {
		baseInfoService.updateValByKey(baseInfo) ;
		return ResponseData.ok() ;
	}
	
}
